<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div class="login-wrapper">
        <div class="login-container">
            <!-- 左侧：欢迎区域 -->
            <div class="login-left">
                <div class="welcome-title">欢迎登录</div>
                <div class="welcome-subtitle">admin管理系统</div>
                <img src="http://localhost:8888/file/bj2" alt="登录宣传图" />
                <div class="tips">安全高效，轻松管理</div>
            </div>

            <!-- 右侧：登录表单 -->
            <div class="login-right">
                <div class="avatar-container">
                    <el-avatar :size="100" :src="form.avatar || 'http://localhost:8888/file/avatar'" />
                </div>

                <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" style="margin-top: 30px;">
                    <el-form-item label="账号" prop="name">
                        <el-input v-model="form.name" placeholder="请输入用户名/邮箱/手机号" @keyup.enter="Login" />
                    </el-form-item>

                    <el-form-item label="密码" prop="password">
                        <el-input v-model="form.password" type="password" placeholder="请输入密码" @keyup.enter="Login"
                            show-password />
                    </el-form-item>

                    <el-form-item style="text-align: center; margin-top: 20px;">
                        <el-button type="primary" @click="Login" style="width: 100%; padding: 12px;">登录</el-button>
                    </el-form-item>

                    <el-form-item style="text-align: center; margin-top: 15px;">
                        <el-button type="success" @click="Register" style="width: 100%;">注册账号</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>

    </div>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
import api from '@/axios/index'
import { useRouter } from 'vue-router'
const router = useRouter()
import { Store } from '@/store/index'
const store = Store()
const form = reactive({
    name: 'admin',
    password: 'admin',
    avatar: null
})
const rules = {
    name: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
}
const formRef = ref(null)
function Register() {
    router.push({
        path: '/Register'
    })

}
function selectUser() {
    if (form.name != '')
        api.get("/user/getAvatar/" + form.name).then(res => {
         
            console.log(res)
            if (res.code == 200) {
                form.avatar = res.msg
            } else {
                ElMessage.error(res.msg)
            }
        })
}
function Login() {
    formRef.value.validate((valid) => {
        if (valid) {
            if (form.avatar == '')
                selectUser()
            api.post('/user/login', form).then(res => {
             
                if (res.code == 200) {
                    store.setUser(res.data.user)

                    localStorage.setItem("token", res.data.token)
                    ElMessage.success("登录成功")
                    router.push('/')
                    console.log("跳转")
                } else {
                    ElMessage.error(res.msg)
                }
            })
        } else {
            ElMessage.error('请填写完整信息')
        }
    });
}

</script>

<!--  样式 -->
<style scoped>
.login-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100%;
    background-color: #f0f2f5;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    background: url('http://localhost:8888/file/bj');
}

.login-container {
    display: flex;
    width: 80%;
    background-color: #f0f2f5;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
}


.login-left {
    flex: 1;
    background: linear-gradient(135deg, #4a90e2, #5c6bc0);
    color: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    border-radius: 10px 0 0 10px;
}

.login-right {
    flex: 1;
    background: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    border-radius: 0 10px 10px 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.welcome-title {
    font-size: 2.5em;
    font-weight: bold;
    margin-bottom: 10px;
}

.welcome-subtitle {
    font-size: 1.2em;
    opacity: 0.9;
    margin-bottom: 20px;
}

.banner-img {
    width: 200px;
    height: auto;
    margin: 20px 0;
    border-radius: 8px;
}

.tips {
    font-size: 0.9em;
    opacity: 0.8;
    margin-top: 20px;
}

.avatar-container {
    text-align: center;
    margin-bottom: 20px;
}

.el-form {
    width: 100%;
    max-width: 350px;
}
</style>